<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\User as UserModel;
use app\api\model\ProjectUserRoute as ProjectUserRouteModel;
use app\Request;

class User extends Base
{

    /**
     * 根据token获取用户信息
     * @param Request $request
     * @return \think\response\Json
     */
    public function getUserInfo(Request $request)
    {
        try {

            $token = $request->param('token');
            $result = $this->checktoken($token);
            //return json($result);
            if(isset($result['LIST']['ITEM']['USERID'])){
                $empuid = $result['LIST']['ITEM']['USERID'];
                $mobile = $result['LIST']['ITEM']['PHONE'];
                //查询禅道用户中心
                $UserModel = new UserModel();
                $info = $UserModel->alias('u')
                                  ->field('u.id,u.empuid,u.realname,u.nickname,p.id as review_id,p.orgid')
                                  ->leftjoin('ProjectPerOrgPeople p','u.id = p.uid')
                                  ->where("u.empuid","=",$empuid)
                                  ->where('p.kp','>',0)
                                  ->where("u.deleted","=","0")->find();

                if(!$info){
                    $info = $UserModel->alias('u')
                        ->field('u.id,u.empuid,u.realname,u.nickname,p.id as review_id,p.orgid')
                        ->leftjoin('ProjectPerOrgPeople p','u.id = p.uid')
                        ->where("u.empuid","=",$empuid)
                        ->where("u.deleted","=","0")->find();
                }

                $is_admin = 0;
                if($info){
                    $ProjectUserRoute = new ProjectUserRouteModel();
                    $admininfo = $ProjectUserRoute->where('uid','=',$info['id'])->find();
                    if($admininfo){
                        $is_admin = 1;
                        $info['is_admin'] = $is_admin;
                    }else{
                        $info['is_admin'] = $is_admin;
                    }
                }

                if($info){
                    $result = ["status"=>true,"code"=>0,"msg"=>"获取用户信息成功！","data"=>$info];
                }else{
                    $result = ["status"=>false,"code"=>1,"msg"=>"获取用户信息失败！","data"=>$empuid,"mobile"=>$mobile];
                }
                return json($result);
            }else{
                $result = ["status"=>false,"code"=>2,"msg"=>"请检查token是否有效！"];
                return json($result);
            }

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 绑定禅道用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function bindUser(Request $request){
        try {
            $userame = $request->param('username');
            $empuid  = $request->param('empuid');
            $mobile  = $request->param('mobile');
            if(!$userame)  return ['status'=>false,'msg'=>'禅道账号必须'];
            if(!$empuid) return ['status'=>false,'msg'=>'员工工号必须'];
            $userModel = new UserModel();
            $userinfo = $userModel->where('account','=',$userame)->find();
            if(!$userinfo){
                $result = ["status"=>false,"msg"=>"绑定失败！"];
            }
            $user = new UserModel();
            $data['empuid'] = $empuid;
            $data['mobile'] = $mobile;
            $res = $user->where('account',$userame)->save($data);
            if($res!==false){
                $result = ["status"=>true,"msg"=>"绑定成功！"];
            }else{
                $result = ["status"=>true,"msg"=>"绑定失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 管理员列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function manageList(Request $request){
        try {

            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $ProjectUserRoute = new ProjectUserRouteModel();
            $list = $ProjectUserRoute->alias('ur')
                ->field('ur.id,ur.uid,ur.create_time,u.realname')
                ->leftjoin('user u','ur.uid=u.id')
                ->paginate([
                'list_rows'=> $pagesize,
                'var_page' => 'page',
                'page'=>$page
            ]);
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 添加管理员用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function addManageUser(Request $request){
        try {
            $uid = $request->param('uid');
            $data['uid'] = $uid;
            $data['create_time'] = time();
            $ProjectUserRoute = new ProjectUserRouteModel();
            $userinfo = $ProjectUserRoute->where('uid','=',$uid)->find();
            if($userinfo){
                $result = ["status"=>false,"msg"=>"用户已存在！"];
                return json($result);
            }

            $res = $ProjectUserRoute->insertGetId($data);
            if($res){
                $result = ["status"=>true,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"添加失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 删除管理员用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function delManageUser(Request $request){
        try {
            $uid = $request->param('uid');
            $ProjectUserRoute = new ProjectUserRouteModel();
            $res = $ProjectUserRoute->where('uid','=',$uid)->delete();
            if($res){
                $result = ["status"=>true,"msg"=>"删除成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"删除失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }


    /**
     * 禅道用户列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function userList(Request $request){
        try {

            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $UserModel = new UserModel();
            $list = $UserModel->alias('u')
                ->field('u.id,u.empuid,u.account,u.realname')
                ->where('u.deleted','=','0')
                ->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
    /**
     * 添加禅道用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function addUser(Request $request){
        try {
            $data['account'] = $request->param('account');
            $data['realname'] = $request->param('realname');
            $data['role'] = $request->param('role');
            $data['dept'] = $request->param('dept');
            $data['commiter'] = "admin";

            $UserModel = new UserModel();
            //先检查账户是否已经存在
            $r = $UserModel->where('account','=',$data['account'])->where('deleted','<>',1)->find();
            if($r){
                $result_r = ["status"=>false,"msg"=>"用户已存在，请不要重复添加！"];
                return json($result_r);
            }
            $res = $UserModel->insertGetId($data);
            if($res){
                $result = ["status"=>true,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"添加失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 删除禅道用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function delUser(Request $request){
        try {
            $id = $request->param('id');
            $UserModel = new UserModel();
            //$res = $UserModel->where('id','=',$id)->delete();
            $data['deleted'] = 1;
            $res = $UserModel->where('id','=',$id)->save($data);
            if($res!==false){
                $result = ["status"=>true,"msg"=>"删除成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"删除失败！"];
            }
            return json($result);
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
}
